/**
 * @desc 首页
 */

import Taro from "@tarojs/taro"
import { View, Button } from "@tarojs/components"
import { PostItem } from "@/components"
import { randomStr } from "@/utils"

const db = wx.cloud.database()
const clt = db.collection("post")

const getPosts = () => [
    {
        id: "34324sedrf32433re32r",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" }
        ],
        film: null,
        desc: "Nice day.",
        likeUsers: [
            {
                id: "770o93frtw",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            }
        ]
    },
    {
        id: "r2323ewf4r32rf23",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" },
            { id: "sdf2323455g4", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" }
        ],
        film: null,
        desc: "Nice day.",
        likeUsers: [
            {
                id: "770o93frtw",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            }
        ]
    },
    {
        id: "r3ewfr3erf23sdfdf33ff",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" },
            { id: "323r23r", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" },
            { id: "sdf2323455g4", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" }
        ],
        film: null,
        desc: "Nice day.",
        likeUsers: [
            {
                id: "770o93frtw",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            }
        ]
    },
    {
        id: "f564ewfseee",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" },
            { id: "323r23r", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" },
            { id: "dsf498we", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" },
            { id: "sdf2323455g4", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" }
        ],
        film: null,
        desc: "Nice day.",
        likeUsers: [
            {
                id: "770o93frtw",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            }
        ]
    },
    {
        id: "8585858574",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" },
            { id: "9i898i87y6", path: "http://attach.bbs.miui.com/forum/201812/17/212119wrssrn7rabrclr6f.jpg" },
            {
                id: "484sio9id7",
                path: "http://attach.bbs.miui.com/forum/201205/13/203055hedey4kye41kcdzy.jpg"
            },
            {
                id: "uui485sow0",
                path: "http://attachments.gfan.com/forum/201503/23/2022204f3zcy3jj43jy7zd.jpg"
            }
        ],
        film: null,
        desc: "Just about apex heros. The pop game.",
        likeUsers: [
            {
                id: "770o93frtw",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            },
            {
                id: "0po09i8u",
                name: "Doom",
                avatar: "http://images.liqucn.com/img/h1/h965/img201709161714530_info300X300.jpg"
            },
            {
                id: "9i8u7y66t5",
                name: "Kaepont JK",
                avatar: "http://tx.haiqq.com/uploads/allimg/170808/0415151545-6.jpg"
            },
            {
                id: "o0o09inmjsd",
                name: "Doom",
                avatar: "http://images.liqucn.com/img/h1/h965/img201709161714530_info300X300.jpg"
            },
            {
                id: "wefedfew53334",
                name: "Kaepont JK",
                avatar: "http://tx.haiqq.com/uploads/allimg/170808/0415151545-6.jpg"
            },
            {
                id: "23423432werwer",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            },
            {
                id: "werwesdfw3e4324",
                name: "Doom",
                avatar: "http://images.liqucn.com/img/h1/h965/img201709161714530_info300X300.jpg"
            },
            {
                id: "34324wdfwer23rwserfsdrf",
                name: "Kaepont JK",
                avatar: "http://tx.haiqq.com/uploads/allimg/170808/0415151545-6.jpg"
            },
            {
                id: "234wdfw324423sdrf23",
                name: "Doom",
                avatar: "http://images.liqucn.com/img/h1/h965/img201709161714530_info300X300.jpg"
            },
            {
                id: "234234rwer23ref234324",
                name: "Kaepont JK",
                avatar: "http://tx.haiqq.com/uploads/allimg/170808/0415151545-6.jpg"
            }
        ]
    },
    {
        id: "sdk90fi90i23490",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" },
            { id: "9i898i87y6", path: "http://attach.bbs.miui.com/forum/201812/17/212119wrssrn7rabrclr6f.jpg" },
            {
                id: "484sio9id7",
                path: "http://attach.bbs.miui.com/forum/201205/13/203055hedey4kye41kcdzy.jpg"
            },
            {
                id: "asasdqwas4d6wq456d4qw56e456qw4as2d1as23",
                path: "http://attachments.gfan.com/forum/201503/23/2022204f3zcy3jj43jy7zd.jpg"
            },
            {
                id: "asd564qw4as5s5w8d78wq9",
                path: "http://attachments.gfan.com/forum/201503/23/2022204f3zcy3jj43jy7zd.jpg"
            }
        ],
        film: null,
        desc: "Just about apex heros. The pop game.",
        likeUsers: []
    },
    {
        id: "465465a4564564564564d89wqd",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "http://i0.hdslb.com/bfs/article/6ce6fcb70b88c0e1c06b65f6809941af94dd4ad9.jpg" },
            { id: "9i898i87y6", path: "http://attach.bbs.miui.com/forum/201812/17/212119wrssrn7rabrclr6f.jpg" },
            { id: "asdw", path: "http://attach.bbs.miui.com/forum/201812/17/212119wrssrn7rabrclr6f.jpg" },
            {
                id: "484sio9id7",
                path: "http://attach.bbs.miui.com/forum/201205/13/203055hedey4kye41kcdzy.jpg"
            },
            {
                id: "asd465wq4d4564as56dwdwdffff",
                path: "http://attachments.gfan.com/forum/201503/23/2022204f3zcy3jj43jy7zd.jpg"
            },
            {
                id: "as56d456as4d512cc12c2c2c2s2awdaer",
                path: "http://attachments.gfan.com/forum/201503/23/2022204f3zcy3jj43jy7zd.jpg"
            }
        ],
        film: null,
        desc: "Just about apex heros. The pop game.",
        likeUsers: []
    },
    {
        id: "iio9i9iiu8",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-732688.jpg" },
            { id: "9i898i87y6", path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-731780.jpg" },
            {
                id: "484sio9id7",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-748291.jpg"
            },
            {
                id: "sd4fsd4f56ds4f6",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-590821.jpg"
            },
            {
                id: "9ii8s8fewr9fwr3",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-143465.png"
            },
            {
                id: "sdfcsdfsdf4ew54r",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-508768.jpg"
            },
            {
                id: "099fwer3423sdffsfwr23",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-37115.jpg"
            }
        ],
        film: null,
        desc: "Some cool things.",
        likeUsers: [
            {
                id: "770o93frtw",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            },
            {
                id: "0po09i8u",
                name: "Doom",
                avatar: "http://images.liqucn.com/img/h1/h965/img201709161714530_info300X300.jpg"
            },
            {
                id: "9i8u7y66t5",
                name: "Kaepont JK",
                avatar: "http://tx.haiqq.com/uploads/allimg/170808/0415151545-6.jpg"
            }
        ]
    },
    {
        id: "78978564564",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-732688.jpg" },
            { id: "9i898i87y6", path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-731780.jpg" },
            {
                id: "484sio9id7",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-748291.jpg"
            },
            {
                id: "s4df564sd56f4ew6f41x2c3",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-590821.jpg"
            },
            {
                id: "9ii8s8fewr9fwr3",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-143465.png"
            },
            {
                id: "asdqwdf",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-143465.png"
            },
            {
                id: "sdfcsdfsdf4ew54r",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-508768.jpg"
            },
            {
                id: "099fwer3423sdffsfwr23",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-37115.jpg"
            }
        ],
        film: null,
        desc: "Some cool things.",
        likeUsers: [
            {
                id: "770o93frtw",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            },
            {
                id: "0po09i8u",
                name: "Doom",
                avatar: "http://images.liqucn.com/img/h1/h965/img201709161714530_info300X300.jpg"
            },
            {
                id: "9i8u7y66t5",
                name: "Kaepont JK",
                avatar: "http://tx.haiqq.com/uploads/allimg/170808/0415151545-6.jpg"
            }
        ]
    },
    {
        id: "sdfj09j09f2wewe",
        user: {
            id: "414141745",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: [
            { id: "o90o30o2d9", path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-732688.jpg" },
            { id: "9i898i87y6", path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-731780.jpg" },
            {
                id: "484sio9id7",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-748291.jpg"
            },
            {
                id: "78987dd2d2d89s7a9d8",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-590821.jpg"
            },
            {
                id: "9ii8s8fewr9fwr3",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-143465.png"
            },
            {
                id: "asdqwdf",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-143465.png"
            },
            {
                id: "sdfcsdfsdf4ew54r",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-508768.jpg"
            },
            {
                id: "asdase",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-508768.jpg"
            },
            {
                id: "099fwer3423sdffsfwr23",
                path: "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-37115.jpg"
            }
        ],
        film: null,
        desc: "Some cool things.",
        likeUsers: [
            {
                id: "770o93frtw",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            },
            {
                id: "0po09i8u",
                name: "Doom",
                avatar: "http://images.liqucn.com/img/h1/h965/img201709161714530_info300X300.jpg"
            },
            {
                id: "9i8u7y66t5",
                name: "Kaepont JK",
                avatar: "http://tx.haiqq.com/uploads/allimg/170808/0415151545-6.jpg"
            }
        ]
    },
    {
        id: "sdjf0ejf0923jrjsdf9023jrf",
        user: {
            id: "90if9023jkf09we",
            name: "Alex",
            avatar: "http://life.southmoney.com/tuwen/UploadFiles_6871/201808/20180824152413186.jpg"
        },
        date: 1550999591047,
        images: null,
        film: { id: "sdfjoijie32jr0923fsdcvcx", path: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" },
        desc: "Woo, I like it.",
        likeUsers: [
            {
                id: "770o93frtw",
                name: "Hama",
                avatar: "http://wsing.bssdl.kugou.com/55865a68b52b4d9d015c78276df350c7.jpg_350x350.jpg"
            },
            {
                id: "0po09i8u",
                name: "Doom",
                avatar: "http://images.liqucn.com/img/h1/h965/img201709161714530_info300X300.jpg"
            },
            {
                id: "9i8u7y66t5",
                name: "Kaepont JK",
                avatar: "http://tx.haiqq.com/uploads/allimg/170808/0415151545-6.jpg"
            }
        ]
    }
]

class Home extends Taro.Component {
    config = {
        enablePullDownRefresh: true
    }

    state = { postList: [] }

    componentDidMount() {
        this.getPostList()
    }

    getPostList() {
        this.setState({ postList: getPosts() })
    }

    onPullDownRefresh() {
        const t = setTimeout(() => {
            console.log("sfsdfsdfd")
            Taro.stopPullDownRefresh()
            clearTimeout(t)
        }, 2000)
    }

    onReachBottom() {
        Taro.showLoading({ title: "loading..." })
        const t = setTimeout(() => {
            Taro.hideLoading()
            clearTimeout(t)
        }, 2000)
    }

    onGet() {
        clt.get().then(res => {
            console.log("got", res)
        })
    }

    onUpdate() {
        clt.doc("XIYRcJT75u22f0wF")
            .update({
                data: {
                    desc: "test update 000000"
                }
            })
            .then(res => {
                console.log("ok", res)
            })
            .catch(err => {
                console.error(err)
            })
    }

    onInset() {
        db.collection("post")
            .add({
                data: {
                    id: randomStr(),
                    date: new Date(),
                    desc: "hhh",
                    userId: "99999",
                    images: ["10000"]
                }
            })
            .then(res => {
                console.log("add", res)
            })
    }

    onRemove() {
        clt.doc("XIYRcJT75u22f0wF")
            .remove()
            .then(res => {
                console.log("remove", res)
            })
            .catch(err => {
                console.error("fail", err)
            })
    }

    render() {
        return (
            <View className="home-page">
                <Button onClick={this.onGet}>get data</Button>
                <Button onClick={this.onUpdate}>update</Button>
                <Button onClick={this.onInset}>inset</Button>
                <Button onClick={this.onRemove}>remove</Button>

                {postList.map(v => (
                    <PostItem key={v.id} data={v} />
                ))}
            </View>
        )
    }
}

export default Home
